package net.dgg.zz.credential.queryCertificate.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.tmd.foundation.platform.annotation.AuthOpt;
import net.dgg.zz.common.constant.CredentialConstant;
import net.dgg.zz.common.service.CommonService;
import net.dgg.zz.common.utils.DataTableResponse;
import net.dgg.zz.common.utils.DateUtils;
import net.dgg.zz.common.utils.excel.ExcelUtils;
import net.dgg.zz.credential.queryCertificate.dto.QueryCertificateDto;
import net.dgg.zz.credential.queryCertificate.service.QueryCertificateService;
import net.fblock.log.NLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zsx
 * @Title: QueryCertificateController
 * @ProjectName zz-sc-zs
 * @Description: 查询证书
 * @date 2018/8/28 14:05
 */
@RequestMapping("/zs/query_certificate")
@Controller
public class QueryCertificateController extends DggBaseController {
    @Autowired
    private QueryCertificateService queryCertificateService;
    @Autowired
    private CommonService commonService;

    /*
     * @Author zsx
     * @Date 2018-08-28 02:08:53
     * @Description 初始化查询证书页面
     */
    @RequestMapping("/index")
    @AuthOpt(code = "ZZ_QUERY_CERTIFICATE ")
    public String index(Model model) {
        List<Map> outboundReason = commonService.queryTreeBook(CredentialConstant.OUT_GOING_REASON, 1, 2, null);
        model.addAttribute("outboundReason", outboundReason);
        model.addAttribute("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        return "zzsc/credential/queryCertificate/index";
    }

    /*
     * @Author zsx
     * @Date 2018-08-28 04:08:26
     * @Description 分页获取查询证书数据
     * @param params
     * @return DataTableResponse
     */
    @RequestMapping("/query_certificate")
    @ResponseBody
    @NLog
    public DataTableResponse queryCertificate(@RequestParam Map<String, Object> params, HttpServletRequest request) {
        try {
            DataTableRequest dt = DataTableUtils.getParam(request);
            params.put("page", dt.getPage());
            params.put("limit", dt.getLength());
            List<QueryCertificateDto> queryCertificates = queryCertificateService.selectCertificateByWithPage(params);
            return new DataTableResponse().data(params, queryCertificates);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new DataTableResponse().data(params, null);
        }
    }

    /*
     * @Author zsx
     * @Date 2018-08-28 05:08:24
     * @Description 导出查询证书
     */
    @RequestMapping("/export_qyery_certificate")
    @ResponseBody
    @NLog
    public void exportQyeryCertificate(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        try {
            List<QueryCertificateDto> queryCertificateDtos = queryCertificateService.selectExportCertificate(params);
            for (QueryCertificateDto dto:queryCertificateDtos){
                dto.setCertificateArea((dto.getAreaProvince()==null?"":dto.getAreaProvince())+"/"+(dto.getCertificateArea()==null?"":dto.getCertificateArea()));
            }
            String fileName = "查询证书" + DateUtils.dateString(new Date(), DateUtils.yyyy_MM_dd_HH_mm_ss) + ".xlsx";
            ExcelUtils.writeToFile(queryCertificateDtos, null, fileName, response,QueryCertificateDto.class);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    /*
     * @Author zsx
     * @Date 2018-08-29 08:08:43
     * @Description
     * @param remark 备注
     * @param referredPeople 转交人
     * @param baseId 证书id
     * @return DggRestResponse
     */
    @RequestMapping("/transfer_certificate")
    @ResponseBody
    @NLog
    public DggRestResponse transferCertificate(Long referredPeople, Long baseId, String remark) {
        try {
            if (null == referredPeople) {
                return this.getFailResponse("移交人不能为空");
            }
            if (null == baseId) {
                return this.getFailResponse("必须选择证书才能进行移交");
            }
            Map<String, Object> map = queryCertificateService.transferCertificate(referredPeople, baseId, remark);
            if ((Boolean) map.get("status")) {
                return this.getSuccessResponse("");
            } else {
                return this.getFailResponse(map.get("msg").toString());
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return this.getFailResponse("服务器错误，请联系管理员");
        }
    }


}
